﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Dracham.Classes
{
    public enum ObjectType
    {
        Creature,
        NPC
    }

    public class Object
    {
        public delegate void ObjectExpiredDelegate();
        public event ObjectExpiredDelegate OnObjectExpire;

        public Guid Id { get; set; }
        public Position Position { get; set; }
        public Boolean IsAttackable { get; set; }
        public Boolean IsSolid { get; set; }
        public Boolean HasExpired { get; set; }

        public double Distance(Object o)
        {
            return this.Distance(o.Position);
        }

        public double Distance(Position p)
        {
            if (p.ZoneId == this.Position.ZoneId)
            {
                float deltaX = p.X - this.Position.X;
                float deltaY = p.Y - this.Position.Y;

                double hypotenuse = Convert.ToDouble(deltaX * deltaX + deltaY * deltaY);

                return Math.Sqrt(hypotenuse);
            }

            return double.NaN;
        }

        public double Distance(float x, float y)
        {
            float deltaX = x - this.Position.X;
            float deltaY = y - this.Position.Y;

            double hypotenuse = Convert.ToDouble(deltaX * deltaX + deltaY * deltaY);

            return Math.Sqrt(hypotenuse);
        }

        public virtual void Expire()
        {
            this.HasExpired = true;

            if (this.OnObjectExpire != null)
                this.OnObjectExpire();

            this.Position.Zone.Objects.Remove(this);
        }

        public void SendPartialData(DrachamNetwork client, string propName)
        {
            client.Invoke(MessageType.PartialCharacterData,
                new NetworkParameter(propName.Length.ToString(), 5),
                new NetworkParameter(propName, propName.Length),
                new NetworkParameter(this.GetType().GetProperty(propName).GetValue(this, null).ToString().Length.ToString(), 5),
                new NetworkParameter(this.GetType().GetProperty(propName).GetValue(this, null).ToString(),
                    this.GetType().GetProperty(propName).GetValue(this, null).ToString().Length));
        }

        public virtual void Initialize()
        {
            this.Id = Guid.NewGuid();
        }
    }
}
